import { login,reg,getUserInfo,logout} from '@/api/user/base'


export default {
    state :{
        addRoutes:[],
        menu: localStorage.getItem("menu")!= '' ? JSON.parse(localStorage.getItem("menu")) : '',
        sec_menu: localStorage.getItem("sec_menu")!= '' ? JSON.parse(localStorage.getItem("sec_menu")) : [],
        actions:[],
        user_id: localStorage.getItem("user_id"),
        nickname: localStorage.getItem("nickname"),
        name: localStorage.getItem("name"),
        avatar: localStorage.getItem("avatar"),
        user_status: '',
        site_title: '',
        show_notice: false,
        loginStatus: localStorage.getItem("Authorization")?1:0,
    },
    mutations :{
        setLoginStatus(state,loginStatus) {
            state.loginStatus = loginStatus
        },
        setRoute(state,route){
            state.addRoutes = route
        },
        setMenu(state, menu) {
            state.menu = menu
        },
        setSecMenu(state, secMenu) {
            localStorage.setItem('sec_menu', JSON.stringify(secMenu))
            state.sec_menu = secMenu
        },
        setActions(state,actions){
            state.actions = actions
        },
        setUserInfo(state,val){
            state.user_id = val.user_id
            state.nickname = val.nickname
            state.name = val.name
            state.avatar = val.avatar
            state.user_status = val.status
        },
        setAvatar(state, val) {
            state.avatar = val
        }
    },
    actions :{
        setUserAvatar({commit}, avatar) {
            commit('setAvatar',avatar)
        },
        setLoginStatus({commit}, loginStatus) {
            commit('setLoginStatus',loginStatus)
        },
        setSecMenu({commit}, secMenu) {
            commit('setSecMenu',secMenu)
        },
        frontReg({commit},userInfo) {
            return new Promise((resolve, reject) => {
                reg(userInfo).then(res => {
                    if (res.code == 200) {
                        localStorage.setItem("Authorization", res.data.token)
                        localStorage.setItem("avatar", res.data.avatar)
                        localStorage.setItem("nickname", res.data.nickname)
                        localStorage.setItem("name", res.data.name)
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        frontLogin({commit},userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo).then(res => {
                    if (res.code == 200) {
                        localStorage.setItem("Authorization", res.token)
                        localStorage.setItem("avatar", res.avatar)
                        localStorage.setItem("nickname", res.nickname)
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        getUserInfo({commit}){
            return new Promise((resolve, reject) => {
                getUserInfo({device:'1'}).then(res => {
                    commit('setUserInfo',res.data)
                    commit('setLoginStatus', 1)
                    if (res.menu.length > 0) {
                        localStorage.setItem('menu_index', res.menu[0].path)
                        localStorage.setItem("menu", JSON.stringify(res.menu))
                        commit('setMenu',res.menu)
                    }

                    /*if(res.status == 200){
                        let routes = res.components
                        let baseRoutes = [
                            {
                                path:'/',
                                component: (resolve) => require(['@/layout/user/default/Main'], resolve),
                                children:[]
                            },
                        ]
                        routes.forEach(item =>{
                            if(item.component_path){
                                item.component = (resolve) => require([`@/views/${item.component_path}`],resolve)
                                baseRoutes[0].children.push(item)
                            }
                        })
                        commit('setRoute',baseRoutes.concat({path:'*',redirect:'/404'}))
                        commit('setMenu',res.menu)
                        commit('setActions',res.actions)
                        commit('setUserInfo',res.data)
                    }*/
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        logout({commit}){
            return new Promise((resolve, reject) => {
                const userToken = localStorage.getItem("Authorization")
                logout({userToken:userToken}).then(res => {
                    if(res.code == 0){
                        localStorage.removeItem("Authorization")
                        localStorage.removeItem("user_id")
                        localStorage.removeItem("menu")

                        // commit('setRoute',[])
                        // commit('setMenu',[])
                        // commit('setActions',[])
                        commit('setUserInfo',{})
                        commit('setLoginStatus', 0)
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        locationLogout({commit}){
            return new Promise((resolve, reject) => {
                localStorage.setItem("Authorization", "")
                localStorage.setItem("user_id", "")
                localStorage.setItem("menu", "")
                commit('setUserInfo',{})
                commit('setLoginStatus', 0)
                resolve({})
            })
        }
        
    }
}